package com.szqhyxj.youxiangju.biz.partnerHome.home.teamperformanreceipt;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.EditText;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.partnerHome.home.RefreshItemDecoration;
import com.szqhyxj.youxiangju.biz.partnerHome.home.partnerdetails.PartnerDetailsActivity;
import com.szqhyxj.youxiangju.biz.partnerHome.home.teamperformancerepayment.RefreshAdapter;
import com.szqhyxj.youxiangju.biz.partnerHome.home.teamperformancerepayment.RowsItem;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by leung on 2018/11/8.
 */

public class TeamPerformanceReceiptActivity extends BaseActivity implements NavigationBar.NavigationBarListener, TeamPerformanceReceiptContract.View ,ReceiptRefreshAdapter.ItemClickListener {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.searchName)
    EditText searchName;
    @BindView(R.id.search)
    TextView search;
    @BindView(R.id.teamperformancedata)
    RecyclerView teamperformancedata;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    ReceiptRefreshAdapter adapter;
    private LinearLayoutManager mLinearLayoutManager;
    TeamPerformanceReceiptPresenter presenter;

    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, TeamPerformanceReceiptActivity.class);
        activity.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_performance_receipt);
        ButterKnife.bind(this);
        presenter = new TeamPerformanceReceiptPresenter(this);
        presenter.init(1,1,10,1);
        initListener();
        swipeLayout.setEnabled(false);
    }
    private void initListener() {

        initLoadMoreListener();

    }
    private void initLoadMoreListener() {

        teamperformancedata.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                //判断RecyclerView的状态 是空闲时，同时，是最后一个可见的ITEM时才加载
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {

                    //设置正在加载更多
                    adapter.changeMoreStatus(adapter.LOADING_MORE);

                    //改为网络请求
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            presenter.load();

                        }
                    }, 3000);


                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //最后一个可见的ITEM
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });

    }


    @Override
    public void showData(List<ReceiptRowsItem> list) {
        adapter = new ReceiptRefreshAdapter(this, list,this);
        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);


        //添加动画
        teamperformancedata.setItemAnimator(new DefaultItemAnimator());

        //添加分割线
        teamperformancedata.addItemDecoration(new RefreshItemDecoration(this, RefreshItemDecoration.VERTICAL_LIST));

        teamperformancedata.setLayoutManager(mLinearLayoutManager);
        teamperformancedata.setAdapter(adapter);
    }

    @Override
    public void showLoad(List<ReceiptRowsItem> list) {
        List<ReceiptRowsItem> footerDatas = new ArrayList<ReceiptRowsItem>();

        adapter.AddFooterItem(list);
        //设置回到上拉加载更多
        adapter.changeMoreStatus(adapter.PULLUP_LOAD_MORE);
        //没有加载更多了
        //mRefreshAdapter.changeMoreStatus(mRefreshAdapter.NO_LOAD_MORE);
    }

    @Override
    public void showNodata() {
        adapter.changeMoreStatus(adapter.NO_LOAD_MORE);
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }

    @Override
    public void onClickItem(String organId) {
        PartnerDetailsActivity.start(this,organId,1,1);
    }
}
